<style scoped>
    .expand-row{
        margin-bottom: 16px;
    }
</style>
<template>
    <can-edit-table
            border
            v-model="list"
            @on-cell-change="handleCellChange"
            @on-change="handleChange"
            :editIncell="true"
            :columns-list="columns"
    ></can-edit-table>
    <!--<Table-->
            <!--ref="selection"-->

            <!--:columns="columns"-->
            <!--:data="list"-->

    <!--&gt;</Table>-->
<!--    <div>
        <Row class="expand-row">
            <i-col span="8">
            <span class="expand-key">Job: </span>
            <span class="expand-value">{{ row.job }}</span>
            </i-col>
            <i-col span="8">
            <span class="expand-key">Interest: </span>
            <span class="expand-value">{{ row.interest }}</span>
            </i-col>
            <i-col span="8">
            <span class="expand-key">Birthday: </span>
            <span class="expand-value">{{ row.birthday }}</span>
            </i-col>
        </Row>
        <Row>
            <i-col span="8">
            <span class="expand-key">Favorite book: </span>
            <span class="expand-value">《{{ row.book }}》</span>
            </i-col>
            <i-col span="8">
            <span class="expand-key">Favorite movie: </span>
            <span class="expand-value">{{ row.movie }}</span>
            </i-col>
            <i-col span="8">
            <span class="expand-key">Favorite music: </span>
            <span class="expand-value">{{ row.music }}</span>
            </i-col>
        </Row>
    </div>-->
</template>
<script>
    import Cookies from 'js-cookie';
    import Util from '../../../libs/util';
    import canEditTable from './canEditTable.vue';
    export default {
        props: {
            row: Object
        },
        components: {
            canEditTable
        },
        data () {
            return {
                columns: [
                    {
                        title: '规格ID',
                        key: 'spec_id'
                    },
                    {
                        title: '规格详情',
                        key: 'spec_name_full',
                    },
                    {
                        title: '单价',
                        key: 'unit_money'
                    },
                    {
                        title: '库存',
                        key: 'canuse_stock'
                    },
                    {
                        title: '销量',
                        key: 'sale_num'
                    },
                    {
                        title: '赠送积分',
                        key: 'point'
                    },
                    {
                        title: '是否上架',
                        key: 'is_show',
                        render: (h, params) => {
                            const color = params.row.is_show==0? 'red': 'green';
                            const text = params.row.is_show==0? '下架': '上架';
                            return h('Tag', {
                                props: {
                                    type: 'dot',
                                    color: color
                                }
                            }, text);
                        }
                    },
                    {
                        title: '采购数量',
                        key: 'num',
                        editable: true
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                            if (this.admin_type != 2) {
                                return;
                            }
                            return h('div', [
                                h('Button', {
                                    props: {
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.del(params.index, params.row.spec_id);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                list: this.row.product_specs,
                admin_type: Cookies.get('admin_type'),
                num: '',
                remark: ''
            };
        },
        methods: {
            handleCellChange (val, index, key) {
                var data = val[index];
                var specId = data.spec_id;
                var num = data.num;
                Util.ajax.post(`/adminapi/product/editpurcart`, {spec_id: specId, num: num}).then(response => {
                    if(response.data.code==0) {
                        if(response.data.data != ''){
                            this.$Message.error('库存不足');
                        }else {
                            this.$Message.success('操作成功');
                        }
                        // this.$emit('getTotalMoney');
                    } else {
                        this.$Message.error('操作失败');
                    }
                });
            },
            handleChange (val, index) {
                // this.editConfig(val[index]);
            },
            del (index, spec_id) {
                let _this = this;
                Util.ajax.post(`/adminapi/product/delpurcart`, {spec_id: spec_id}).then(response => {
                    if(response.data.code==0) {
                        this.list.splice(index, 1);
                        this.$Message.success('操作成功');
                        // this.$emit('getTotalMoney');
                    } else {
                        this.$Message.error('操作失败');
                    }
                });
            }
        }
    };
</script>
